package me.zxk.qwert_poetry.dao;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PoemRepository extends JpaRepository<Poem, Integer> {
    Page<Poem> findAllByContentContainingOrTitleContainingOrAuthor_NameContaining(String content, String title, String author, Pageable pageable);
    Page<Poem> findAllByTags_Name(String name, Pageable pageable);

    @Query(value = "select count(*) from user_like_poem where poem_id = ?1", nativeQuery = true)
    int getLikeCountById(Integer id);

    @Query(value = """
select count(distinct u.user_id) from collection_poem cp
inner join collection c on cp.collection_id = c.collection_id
inner join user u on u.user_id=c.user_id
where poem_id = ?1
""", nativeQuery = true)
    int getCollectCountById(Integer id);

    Page<Poem> findAllByAuthor_Id(int authorId, PageRequest of);

    @Query(value = "select * from poem order by rand() limit 1", nativeQuery = true)
    Poem getRandomPoem();
}
